//
//  PersonGoOutDetailViewController.m
//  MobileOffice
//
//  Created by jjkj on 16/12/15.
//  Copyright © 2016年 liaoningjujin. All rights reserved.
//

#import "PersonGoOutDetailViewController.h"
#import "ImageDetailViewController.h"
#import<BaiduMapAPI_Map/BMKMapView.h>
#import<BaiduMapAPI_Location/BMKLocationService.h>
#import<BaiduMapAPI_Search/BMKGeocodeSearch.h>
#import <BaiduMapAPI_Search/BMKSuggestionSearch.h>
#import<BaiduMapAPI_Search/BMKSuggestionSearchOption.h>
#import <BaiduMapAPI_Map/BMKPinAnnotationView.h>
#import <BaiduMapAPI_Map/BMKPointAnnotation.h>
#import <BaiduMapAPI_Map/BMKPolyline.h>
#import <BaiduMapAPI_Map/BMKPolylineView.h>
#import "UserHeadImageModel.h"
#import "PersonGoOutDetailModel.h"
#import "MyImageView.h"

#define getBusinessRecordLocationInfo @"getBusinessRecordLocationInfo"//公出轨迹

@interface PersonGoOutDetailViewController ()<BMKMapViewDelegate,BMKLocationServiceDelegate,BMKGeoCodeSearchDelegate>
{
    BMKMapView *_mapView;  //地图
    BMKLocationService *_locService;  //定位
    BMKGeoCodeSearch *_geocodesearch; //地理编码主类，用来查询、返回结果信息
    //UserHeadImageModel *_imageModel;//照片model
    BMKPointAnnotation* nowAnnotation;
    BMKPolyline* polyline;
    NSInteger _photoPageCount;//当前图片的页数
    NSInteger _photoNum;//照片的总数量
}
@property (nonatomic,strong) PersonGoOutDetailModel *personModel;
@end

@implementation PersonGoOutDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setLeftBackNavigation];
    [self setNavigationTitle:@"员工动态"];
    [self creatView];
    [self getBusinessRecordLocationInfoRequest];
    // Do any additional setup after loading the view.
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [_mapView viewWillAppear];
    _mapView.delegate = self; // 此处记得不用的时候需要置nil，否则影响内存的释放
//    CLLocationCoordinate2D coordinate=CLLocationCoordinate2DMake([self.addressX doubleValue], [self.addressY doubleValue]);
//    readyAnnotation= [[BMKPointAnnotation alloc]init];
//    readyAnnotation.coordinate = coordinate;
//    readyAnnotation.title = @"";
//    [_mapView addAnnotation:readyAnnotation];
    
}

#pragma mark - creatView

- (void)creatView{
    
    UIView *dateView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, 60)];
    dateView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:dateView];
    
    UILabel *address=[MyControl creatLabelWithFrame:CGRectMake(10, 10, IPHONE_WIDTH-20, 20) text:self.address];
    [dateView addSubview:address];
    //时间的label
    UILabel *time=[MyControl creatLabelWithFrame:CGRectMake(10,address.y+address.height, IPHONE_WIDTH-20, 20) text:self.time];
    [dateView addSubview:time];
    
    //添加地图视图
    _mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, dateView.y+dateView.height, IPHONE_WIDTH, IPHONE_HEIGHT-(dateView.y+dateView.height)-64)];
    _mapView.showsUserLocation = YES; //是否显示定位图层（即我的位置的小圆点）
    _mapView.zoomLevel = 15;//地图显示比例
    _mapView.mapType = BMKMapTypeStandard;//设置地图为空白类型
    _mapView.showMapScaleBar = YES;
    //自定义比例尺的位置
    _mapView.mapScaleBarPosition = CGPointMake(_mapView.frame.size.width - 70, _mapView.frame.size.height - 68);
    [self.view addSubview:_mapView];
    
    BMKLocationViewDisplayParam *displayParam = [[BMKLocationViewDisplayParam alloc]init];
    displayParam.isRotateAngleValid = true;//跟随态旋转角度是否生效
    displayParam.isAccuracyCircleShow = false;//精度圈是否显示
    displayParam.locationViewImgName= @"icon_mark@2x";//定位图标名称
    displayParam.locationViewOffsetX = 0;//定位偏移量(经度)
    displayParam.locationViewOffsetY = 0;//定位偏移量（纬度）
    [_mapView updateLocationViewWithParam:displayParam];
    
    
    _geocodesearch = [[BMKGeoCodeSearch alloc] init];
    _geocodesearch.delegate = self;
      [self startLocation];//开始定位方法
}


-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [_mapView viewWillDisappear];
    _mapView.delegate = nil; // 不用时，置nil
    
}

//开始定位
-(void)startLocation
{
    //初始化BMKLocationService
    _locService = [[BMKLocationService alloc]init];
    _locService.delegate = self;
    _locService.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
    //启动LocationService
    [_locService startUserLocationService];
}

//处理位置坐标更新
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{

    //普通态
    //以下_mapView为BMKMapView对象
    [_mapView updateLocationData:userLocation]; //更新地图上的位置
    _mapView.centerCoordinate = userLocation.location.coordinate; //更新当前位置到地图中间
    //地理反编码
    BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];
    reverseGeocodeSearchOption.reverseGeoPoint = userLocation.location.coordinate;
    BOOL flag = [_geocodesearch reverseGeoCode:reverseGeocodeSearchOption];
    
    if(flag){
        [_locService stopUserLocationService];
    }else{
        NSLog(@"反geo检索发送失败");
    }
}

#pragma mark -------------地理反编码的delegate---------------
-(void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{
    //NSLog(@"address:%@----%@",result.addressDetail,result.address);
    [_mapView removeAnnotation:nowAnnotation];
    //    //增加气泡
    nowAnnotation = [[BMKPointAnnotation alloc]init];
    nowAnnotation.coordinate = result.location;
    nowAnnotation.title = result.address;
    [_mapView addAnnotation:nowAnnotation];
    //        _mapView.centerCoordinate = result.location;
    //addressDetail:     层次化地址信息
    //address:    地址名称
    //businessCircle:  商圈名称
    // location:  地址坐标
    //  poiList:   地址周边POI信息，成员类型为BMKPoiInfo
}



//根据anntation生成对应的View
- (BMKAnnotationView *)mapView:(BMKMapView *)view viewForAnnotation:(id <BMKAnnotation>)annotation
{
    if (annotation==nowAnnotation) {
        NSString *AnnotationViewID = @"annotationViewID";
        //根据指定标识查找一个可被复用的标注View，一般在delegate中使用，用此函数来代替新申请一个View
        BMKAnnotationView *annotationView = [view dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
        if (annotationView == nil) {
            annotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
            ((BMKPinAnnotationView*)annotationView).pinColor = BMKPinAnnotationColorRed;
            ((BMKPinAnnotationView*)annotationView).animatesDrop = NO;
            annotationView.image=[UIImage imageNamed:@"icon_mark@2x"];
        }
        
        annotationView.centerOffset = CGPointMake(0, -(annotationView.frame.size.height * 0.5));
        annotationView.annotation = annotation;
        annotationView.canShowCallout = YES;
        //添加文字说明
        UIView *backView=[[UIView alloc]initWithFrame:CGRectMake((60-annotationView.center.x)/2, annotationView.height, 60, 20)];
        backView.center=CGPointMake(annotationView.center.x+5, annotationView.height+10);
        backView.backgroundColor=[UIColor clearColor];
        [annotationView addSubview:backView];
        
        UIView *alphoView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, backView.width, backView.height)];
        alphoView.alpha=0.1;
        alphoView.backgroundColor=[UIColor blackColor];
        [backView addSubview:alphoView];
        
        UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, backView.width, backView.height)];
        label.backgroundColor=[UIColor clearColor];
        label.font=FONT_13;
        label.textAlignment=NSTextAlignmentCenter;
        label.text=@"当前位置";
        [backView addSubview:label];
        return annotationView;
    }
    NSString *AnnotationViewID = @"annotation";
    //根据指定标识查找一个可被复用的标注View，一般在delegate中使用，用此函数来代替新申请一个View
    BMKAnnotationView *annotationView = [view dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
    if (annotationView == nil) {
        annotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
        ((BMKPinAnnotationView*)annotationView).pinColor = BMKPinAnnotationColorRed;
        ((BMKPinAnnotationView*)annotationView).animatesDrop = NO;
        annotationView.image=[UIImage imageNamed:@"k_current@2x"];
    }
    
    annotationView.centerOffset = CGPointMake(0, -(annotationView.frame.size.height * 0.5));
    annotationView.annotation = annotation;
    annotationView.canShowCallout = YES;

    
    
    MyImageView *imageview=[[MyImageView alloc]initWithFrame:CGRectMake(0, 0, 110, 110)];

    //遍历找到对应的model
    for (PersonGoOutDetailModel *model in self.personModel.dataArr) {
        if ([model.latitude doubleValue]==annotation.coordinate.latitude) {
            [self addTextInView:annotationView text:[LZXHelper getTimeStrWithOldDateFomatter:@"yyyy-MM-dd HH:mm:ss" DateStr:model.locationTime newFormatter:@"HH:mm"] backgroundColor:[UIColor blackColor] textColor:[UIColor blackColor]];
            NSString *urlStr=model.picPath;
            if (urlStr.length>0&&urlStr!=nil) {
                //去掉前面的|
                if ([[urlStr substringToIndex:1]isEqualToString:@"|"]) {
                    urlStr=[urlStr substringFromIndex:1];
                }
                //去掉最后面的|
                if ([[urlStr substringFromIndex:urlStr.length-1]isEqualToString:@"|"]) {
                    urlStr =[urlStr substringToIndex:urlStr.length-1];
                }
                imageview.urlStr=urlStr;
                imageview.nameStr=model.OBAddress;
                imageview.timeStr=model.locationTime;
                NSArray *infoArr=[urlStr componentsSeparatedByString:@"|"];
                if (infoArr.count>0) {
                    [imageview setImageWithURL:[NSURL URLWithString:infoArr[0]]];
                }

                [imageview addTarget:self action:@selector(photoImageView:)];
                annotationView.leftCalloutAccessoryView=imageview;
            }
            break;
        }
    }
    
    
//    annotationView.image=[self getImageFromView:viewForImage];
    
    return annotationView;

}

// 当点击annotation view弹出的泡泡时，调用此接口
- (void)mapView:(BMKMapView *)mapView annotationViewForBubble:(BMKAnnotationView *)view;
{
    
}

#pragma mark -照片查看
- (void)photoImageView:(MyImageView *)imageView{
    [self creatPhotoViewWithPhotoUrl:imageView.urlStr address:imageView.nameStr time:imageView.timeStr];
}

#pragma mark -照片查看
- (void)creatPhotoViewWithPhotoUrl:(NSString *)photoUrl address:(NSString *)address time:(NSString *)time{
    _photoPageCount=0;
    UIButton *photoView=[MyControl creatButtonWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, IPHONE_HEIGHT) target:self sel:@selector(cancelPhotoClick:) tag:200 image:nil title:@""];
    photoView.backgroundColor=[UIColor clearColor];
    [[[UIApplication sharedApplication].delegate window] addSubview:photoView];
    
    //半透明的view
    UIView *alphaView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, IPHONE_HEIGHT)];
    alphaView.backgroundColor=[UIColor blackColor];
    alphaView.alpha=0.5;
    alphaView.userInteractionEnabled=NO;
    [photoView addSubview:alphaView];
    
    NSArray *infoArr=[photoUrl componentsSeparatedByString:@"|"];
    _photoNum=infoArr.count;
    //滚动视图
    UIScrollView *scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, (IPHONE_HEIGHT-(IPHONE_WIDTH-40-60))/2, IPHONE_WIDTH, IPHONE_WIDTH-40-60)];
    scrollView.contentSize=CGSizeMake(IPHONE_WIDTH*infoArr.count, 0);
    scrollView.tag=180;
    scrollView.bounces=NO;
    scrollView.userInteractionEnabled=NO;
    scrollView.pagingEnabled=YES;
    scrollView.scrollEnabled=NO;
    [photoView addSubview:scrollView];
    for (int i=0; i<infoArr.count; i++) {
        //照片
        UIImageView *photo=[[UIImageView alloc]initWithFrame:CGRectMake(20+scrollView.width*i, 0, IPHONE_WIDTH-40, scrollView.height)];
        [photo setImageWithURL:[NSURL URLWithString:infoArr[i]] placeholderImage:[UIImage imageNamed:@"ic_pic_bg@2x"]];
        [scrollView addSubview:photo];
    }
    //如果不止一张图片 则添加向左向右按钮
    if (infoArr.count>1) {
        UIButton *leftButton=[MyControl creatButtonWithFrame:CGRectMake(5, scrollView.y+(scrollView.height-40)/2, 50, 50) target:self sel:@selector(leftPhotoClick:) tag:220 image:nil title:@""];
        [leftButton setImage:[UIImage imageNamed:@"jiantou_left@2x"] forState:UIControlStateNormal];
        leftButton.layer.masksToBounds=YES;
        leftButton.layer.cornerRadius=leftButton.width/2;
        leftButton.backgroundColor=[UIColor whiteColor];
        leftButton.hidden=YES;
        leftButton.alpha=0.8;
        [photoView addSubview:leftButton];
        
        UIButton *rightButton=[MyControl creatButtonWithFrame:CGRectMake(IPHONE_WIDTH-5-50,scrollView.y+(scrollView.height-50)/2, 50, 50) target:self sel:@selector(rightPhotoClick:) tag:221 image:nil title:@""];
        rightButton.layer.masksToBounds=YES;
        [rightButton setImage:[UIImage imageNamed:@"jiantou_right@2x"] forState:UIControlStateNormal];
        rightButton.layer.cornerRadius=leftButton.width/2;
        rightButton.backgroundColor=[UIColor whiteColor];
        rightButton.alpha=0.8;
        [photoView addSubview:rightButton];
    }
    
        //图片信息背景
        UIView *titleView=[[UIView alloc]initWithFrame:CGRectMake(20, IPHONE_HEIGHT-(35+50), IPHONE_WIDTH-40, 50)];
        titleView.alpha=0.5;
        titleView.userInteractionEnabled=NO;
        titleView.backgroundColor=[UIColor blackColor];
        [photoView addSubview:titleView];
    
        //地点
        UILabel *addressLabel=[MyControl creatLabelWithFrame:CGRectMake(60, titleView.y+5, titleView.width-120, 20) text:[NSString stringWithFormat:@"地点:%@",address]];
        addressLabel.font=FONT_13;
        addressLabel.textColor=[UIColor whiteColor];
        //计算高度
        CGFloat addressHeight=[LZXHelper textHeightFromTextString:addressLabel.text width:addressLabel.width fontSize:13]+3;
        addressLabel.height=addressHeight;
        [photoView addSubview:addressLabel];
        //时间
        UILabel *timeLabel=[MyControl creatLabelWithFrame:CGRectMake(60, addressLabel.y+addressLabel.height, titleView.width-120, 20) text:[NSString stringWithFormat:@"时间:%@",time]];
        timeLabel.textColor=[UIColor whiteColor];
        timeLabel.font=FONT_13;
        [photoView addSubview:timeLabel];
        titleView.height=30+addressHeight;
}

#pragma mark -照片查看取消
- (void)cancelPhotoClick:(UIButton *)button{
    [button removeFromSuperview];
}

#pragma mark -查看照片向左按钮
- (void)leftPhotoClick:(UIButton *)button{
    _photoPageCount--;
    UIButton *photoButton=(UIButton *)button.superview;
    UIScrollView *scrolleView=(UIScrollView *)[photoButton viewWithTag:180];
    //滚动照片
    scrolleView.contentOffset=CGPointMake(_photoPageCount*IPHONE_WIDTH, 0);
    UIButton *leftButton=(UIButton *)[photoButton viewWithTag:220];
    UIButton *rightButton=(UIButton *)[photoButton viewWithTag:221];
    //    if (_photoPageCount==_photoNum) {
    //        rightButton.hidden=YES;
    //    }else{
    rightButton.hidden=NO;
    //    }
    if (_photoPageCount==0) {
        
        leftButton.hidden=YES;
    }else{
        leftButton.hidden=NO;
    }
}

#pragma mark -查看照片向右按钮
- (void)rightPhotoClick:(UIButton *)button{
    _photoPageCount++;
    UIButton *photoButton=(UIButton *)button.superview;
    UIScrollView *scrolleView=(UIScrollView *)[photoButton viewWithTag:180];
    //滚动照片
    scrolleView.contentOffset=CGPointMake(_photoPageCount*IPHONE_WIDTH, 0);
    UIButton *rightButton=(UIButton *)[photoButton viewWithTag:221];
    UIButton *leftButton=(UIButton *)[photoButton viewWithTag:220];
    //    if (_photoPageCount==0) {
    //        leftButton.hidden=YES;
    //    }else{
    leftButton.hidden=NO;
    //    }
    if (_photoPageCount==_photoNum-1) {
        rightButton.hidden=YES;
    }else{
        rightButton.hidden=NO;
    }
}


//-(UIImage *)getImageFromView:(UIView *)view{
//    
//    UIGraphicsBeginImageContext(view.bounds.size);
//    
//    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
//    
//    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
//    
//    UIGraphicsEndImageContext();
//    
//    return image;
//    
//}

#pragma mark -气泡文字提示
- (void)addTextInView:(BMKAnnotationView *)annotationView text:(NSString *)text backgroundColor:(UIColor *)backgroundColor textColor:(UIColor *)textColor{
    //添加文字说明
    UIView *backView=[[UIView alloc]initWithFrame:CGRectMake(annotationView.width, 0, 40, 20)];
    backView.backgroundColor=[UIColor clearColor];
    [annotationView addSubview:backView];
    
    UIView *alphoView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, backView.width, backView.height)];
    alphoView.alpha=0.1;
    alphoView.backgroundColor=backgroundColor;
    [backView addSubview:alphoView];
    
    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, backView.width, backView.height)];
    label.backgroundColor=[UIColor clearColor];
    label.font=FONT_13;
    label.textAlignment=NSTextAlignmentCenter;
    label.text=text;
    [backView addSubview:label];
}



//根据overlay生成对应的View
- (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay{
    if ([overlay isKindOfClass:[BMKPolyline class]]){
        BMKPolylineView* polylineView = [[BMKPolylineView alloc] initWithOverlay:overlay];
        polylineView.strokeColor = [[UIColor redColor] colorWithAlphaComponent:1];//颜色
        polylineView.lineWidth = 2.0;//宽度
        return polylineView;
    }
    return nil;
}

#pragma mark -公出轨迹
- (void)getBusinessRecordLocationInfoRequest{
    

    //参数
    NSDictionary *dict=@{
                         @"id":self.vacateBillId?self.vacateBillId:@"",
                         };
    
    //   字典转json
    NSData* jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
    NSString* str = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    //拼接soap字符串
    NSString* soap = [NSString stringWithFormat:@"<sessionId>%@</sessionId>\n<content>%@</content>",[LZXHelper getSessionID],str];
    
    //[LZXHelper postLoadViewWithText:HUD_PLEASEWAIT View:self.view];
    
    [HttpRequest SOAPData:[LZXHelper getServerURL] SoapMthod:getBusinessRecordLocationInfo soapBody:soap success:^(id responseObject) {
        //[LZXHelper hideLoadViewWithView:self.view];
        //解析数据并保存用户数据
        //NSLog(@"%@",responseObject[@"msg"]);
        self.personModel=[[PersonGoOutDetailModel alloc]initWithResponse:responseObject];
        if ([self.personModel.code isEqualToString:@"200"]) {
            //添加折线覆盖物
            if (polyline == nil) {
                NSInteger count=self.personModel.dataArr.count;
                CLLocationCoordinate2D * coors = (CLLocationCoordinate2D *)malloc(count * sizeof(CLLocationCoordinate2D));
                for (int i=0 ;i<self.personModel.dataArr.count;i++) {
                    PersonGoOutDetailModel *personModel=self.personModel.dataArr[i];
                    coors[i].latitude = [personModel.latitude doubleValue];
                    coors[i].longitude = [personModel.longitude doubleValue];
                    
                    BMKPointAnnotation  *readyAnnotation = [[BMKPointAnnotation alloc]init];
                    CLLocationCoordinate2D coordinate=CLLocationCoordinate2DMake([personModel.latitude doubleValue], [personModel.longitude doubleValue]);
                    readyAnnotation.coordinate = coordinate;
                    readyAnnotation.title = personModel.OBAddress;
                    [_mapView addAnnotation:readyAnnotation];
                    if (i==0) {
                        _mapView.centerCoordinate = coordinate;
                    }
                }
                
                polyline = [BMKPolyline polylineWithCoordinates:coors count:count];
            }
            [_mapView addOverlay:polyline];
            
            //计算最小经纬度和最大经纬度两点之间的距离
            int dictance = [self getDistanceWithArr:self.personModel.dataArr];
            //这个数组就是百度地图比例尺对应的物理距离，其中2000000对应的比例是3，5对应的是21；可能有出入可以根据情况累加
            NSArray *zoomLevelArr = [[NSArray alloc]initWithObjects:@"2000000", @"1000000", @"500000", @"200000", @"100000", @"50000", @"25000", @"20000", @"10000", @"5000", @"2000", @"1000", @"500", @"200", @"100", @"50", @"20", @"10", @"5", nil];
            for (int j=0; j<zoomLevelArr.count; j++) {
                if (j + 1 < zoomLevelArr.count) {
                    if (dictance < [zoomLevelArr[j] intValue] && dictance > [zoomLevelArr[j+1] intValue] ) {
                        [_mapView setZoomLevel:j+6];
                        break;
                    }
                }    
            }
        }else if([self.personModel.code isEqualToString:@"220"]){
            [LZXHelper postMessageWithText:self.personModel.msg View:self.view];
        }

    } failure:^(NSError *error) {
        //[LZXHelper hideLoadViewWithView:self.view];
    }];
    
}

#pragma mark -计算最远的距离
- (double)getDistanceWithArr:(NSMutableArray *)arr{
    CGFloat maxValue=0;
    CGFloat minValue=1000000;
    CGFloat maxLatitude = 0.0;//用来记录哪个经度最大
    CGFloat maxLongitude = 0.0;//用来记录哪个纬度最大
    CGFloat minLatitude = 0.0;//用来记录哪个经度最小
    CGFloat minLongitude = 0.0;//用来记录哪个纬度最小
    for (int i=0; i<arr.count; i++) {
        PersonGoOutDetailModel *model=arr[i];
        CGFloat value=[model.latitude doubleValue]*[model.longitude doubleValue];
        if (i==0) {
            minValue=value;
        }
        if (maxValue<value) {
            maxLatitude=[model.latitude doubleValue];
            maxLongitude=[model.longitude doubleValue];
            maxValue=value;
        }
        if (minValue>value) {
            minLatitude=[model.latitude doubleValue];
            minLongitude=[model.longitude doubleValue];
            minValue=i;
            minValue=value;
        }
    }
    //第一个坐标
    
    CLLocation *current=[[CLLocation alloc] initWithLatitude:maxLatitude longitude:maxLongitude];
    
    //第二个坐标
    
    CLLocation *before=[[CLLocation alloc] initWithLatitude:minLatitude longitude:minLongitude];
    CLLocationDistance meters=[current distanceFromLocation:before];
    return meters;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
